home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5150 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.8 KB  |  65 lines

  1. Path: lrz-muenchen.de!sun2!ua302aa
  2. From: ua302aa@sun2.lrz-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Command line Arguments
  5. Date: 4 Feb 1996 21:54:17 GMT
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Distribution: world
  8. Message-ID: <4f39u9$2b6@sparcserver.lrz-muenchen.de>
  9. References: <4f2qev$9jq@cloner3.netcom.com>
  10. NNTP-Posting-Host: sun2.lrz-muenchen.de
  11.  
  12. buxx@ix.netcom.com(Glen 'Steve' Vandiver ) writes:
  13.  
  14. >Hi! I have been haveing trouble with my commandline arguements. I have
  15. >it too where i can read the entire argument string after the run. No
  16. >prob. but lets say i want it to split it up like this. the first word
  17. >goes into char *user; and the rest goes into char *command;. HOW WOULD
  18. >I DO THIS? thanx! bye!
  19.  
  20. You could do something like
  21. ----------------8<--------------------8<-------------------------
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24.  
  25. int
  26. main(int argc, char **argv)
  27. {
  28.    register int i;
  29.    char *user;
  30.    char *cmd = 0;
  31.    size_t len = 0;
  32.  
  33.    if (argc < 3)
  34.       exit(EXIT_FAILURE);
  35.    user = argv[1];
  36.    for (i = 2; i < argc; ++i) {
  37.       len += strlen(argv[i]) + 1;
  38.       cmd = realloc(cmd, len);
  39.       if (cmd == NULL)
  40.          exit(EXIT_FAILURE);
  41.       if (i == 2) {
  42.          strcpy(cmd, argv[i]);
  43.       } else {
  44.          strcat(cmd, " ");
  45.          strcat(cmd, argv[i]);
  46.       }  
  47.    }   
  48.    printf("call(\"%s\", \"%s\")\n", user, cmd);
  49.    return EXIT_SUCCESS;
  50. }
  51. ----------------8<--------------------8<-------------------------
  52. but most operating environments I know support a method of passing
  53. more than on "word" as one argument to a program, e.g. something
  54. like making your user enter
  55.  
  56.    call myprg user "do what ever you want"
  57.  
  58. might result in myprog being called with two arguments.
  59.  
  60. Kurt
  61. --
  62. | Kurt Watzka                             Phone : +49-89-2180-6254
  63. | watzka@stat.uni-muenchen.de
  64. | ua302aa@sunmail.lrz-muenchen.de
  65.